import { defineStore } from "pinia";
import { hallIntro } from "../api/casion.js";
import {
  getCommonRegisterConfig,
  getGoogleCode,
  touristLogin,
  userDetail
} from "../api/user.js";
import { ElMessage } from "element-plus";
import i18n from "@/lang";
import router from "@/router/index.js";
export const mainAct = defineStore("main", {
  state: () => {
    return {
      showPhoneMenu: false, //手机端侧边导航(全屏) flag
      isLogin: localStorage.getItem("isLogin") || false, //是否登录标识
      user: {
        rechargStatus: true
      },
      vipinfo: {
        created_at: ""
      },
      user_detail: null,
      token: "",
      static_base_url: "",
      lang_support_list: {},
      currency: "", //当前厅币种
      active_list: [], // 活动列表
      loginLayer: false, //登录弹窗
      registerActLayer: false, //注册弹窗
      checkActLayer: false,
      fst_request: true, //首次请求
      showCheckIcon: false, // 显示点击icon
      showFootTable: true, //显示 页脚上方排行榜
      recentList: [], //近期游戏记录
      isLoading: false, // loading组件flag
      logo: "", // 站点logo
      htmlInfo: "", // 首页介绍
      documentTitle: "", // 标题首页
      favicon: "", // 网页站点 logo
      privacy_policy: "", // 隐私政策
      anti_money: "", // 反洗钱政策
      service_contract: "", // 服务条款
      download: {}, // 服务条款
      svgData: [], // svg图标数据
      currentGame: {}, //当前游戏 iframe
      footerDisPath: ["/myBet", "/phone", "/service", "/sports"], // 控制是否显示footer
      isPhoneModel: false, // 是否是手机模式
      invit_code: "", // 邀请码
      scrollable: null,
      showService: false, // 是否显示客服
      customerService: "", // 客服地址

      timeZone: "", // 时区
      timeZoneObject: {}, // 时区
      menuActiveTab: "recharge", // 手机模式tab选中状态

      isFeePhone: 1, // 是否显示注册时，需要手机号 1 需要  0 不需要
      registerConfig: {}, // 注册配置
      _resitre: false, // 注册弹窗
      forget_pc: false, // 忘记密码弹窗PC
      isApp: sessionStorage.getItem("isApp") || false, // 是否是app
      business_list: [], // 服务商列表
      notifyShow: false, // 是否显示通知
      isTabbar: ["/iframe"],
      _isMobile: false, // 是否是手机
      _isTablet: false, // 是否是平板
      _isDesktop: false, // 是否是电脑
      tourist_user_detail: {}, // 游客登录用户信息
      oauth: {},
      tourist_user_detail: {}, // 游客登录用户信息
      oauth: {}
    };
  },
  getters: {},

  actions: {
    async gethallIntro() {
      function isMobile() {
        return /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent);
    }
    
    const deviceType = isMobile() ? 'mobile' : 'pc';
    console.log('// 输出 ---------- ',deviceType); // 输出 'mobile' 或 'pc'


      hallIntro().then(res => {
        console.log("是否显示注册时，需要手机号 1 需要  0 不需要", res);
        if (res?.errno == 0) {
          // 大厅简介
          this.htmlInfo = res.data.hall_intro?.intro;

          // logo
          this.logo = this.static_base_url + res.data.base_setting.logo;
          this.favicon = this.static_base_url + res.data.base_setting.icon;

          document.title = res.data.base_setting.site_name;
          if(!this.favicon.length) return
          let favicons;
          favicons = document.getElementById("favicon");
          favicons.href = this.favicon;

          // 关于我们
          this.privacy_policy = res.data.privacy_policy?.intro;
          this.anti_money = res.data.anti_money?.intro;
          this.service_contract = res.data.service_contract?.intro;

          this.download = res.data.app_download;
          this.oauth = res.data.oauth;
        }
      });
    },
    // 获取注册时，是否需要手机号
    async getCommonRegisterIsFee() {
      let res = await getCommonRegisterConfig();
      if (res.errno == 0) {
        // this.isFeePhone = res.data.is_fee
        this.registerConfig = res.data;
      }
    },
    // 获取用户信息
    async getUSerDetil() {
      let res = await userDetail();
      if (res.errno == 0) {
        this.user_detail = res.data.detail;
      }
    },
    touristLoginApi() {
      const user_detail = JSON.parse(
        localStorage.getItem("tourist_user_detail")
      );
      touristLogin({
        user_name: user_detail?.tourist_user_detail?.user_name,
        invit_code: this.invit_code
      }).then(res => {
        console.log("isMobile",router, this._isMobile);

        if (res.errno == 0) {
          this.token = res.data.token;
          this.isLogin = true;
          let user_detail = {
            ...res.data.user_detail,
            uid: res.data.user_detail.uid.toString()
          };
          this.user_detail = user_detail;

          this.tourist_user_detail = res.data.user_detail;
          if (this._isMobile) {
            router.push("/");
          } else {
            this.loginLayer = false;
          }
        }
      });
    },
    getGoogleCodeApi(data) {
      getGoogleCode({
        ...data,
        invit_code: this.invit_code
      }).then(res => {
        if (res.errno == 0) {
          this.token = res.data.token;
          this.isLogin = true;
          let user_detail = {
            ...res.data.user_detail,
            uid: res.data.user_detail.uid.toString()
          };
          this.user_detail = user_detail;
          this.loginLayer = false;
          router.push("/");
        }
      });
    },
    alterFooter(val) {
      this.showFootTable = val;
    },
    showLoginLayer(msg) {
      if (msg !== "no") {
        ElMessage.warning(i18n.global.t("layer.login.login_tips"));
      }
      this.loginLayer = true;
    },
    closeLoginLayer() {
      this.loginLayer = false;
      this.closeRegister();
      this.closeForget();
    },
    showRegister() {
      this._resitre = true;
    },
    closeRegister() {
      this._resitre = false;
    },
    notifyClose() {
      this.notifyShow = false;
    },
    showNotify() {
      this.notifyShow = true;
    },
    showForget() {
      this.forget_pc = true;
    },
    closeForget() {
      this.forget_pc = false;
    }
  },
  //持久化
  persist: {
    enabled: true,
    detached: true,
    strategies: [
      {
        key: "showPhoneMenu",
        storage: sessionStorage,
        paths: ["showPhoneMenu"]
      },
      {
        key: "isLogin",
        storage: localStorage,
        paths: ["isLogin"]
      },
      {
        key: "user",
        storage: sessionStorage,
        paths: ["user"]
      },
      {
        key: "vipinfo",
        storage: sessionStorage,
        paths: ["vipinfo"]
      },
      {
        key: "user_detail",
        storage: localStorage,
        paths: ["user_detail"]
      },
      {
        key: "tourist_user_detail",
        storage: localStorage,
        paths: ["tourist_user_detail"]
      },
      {
        key: "token",
        storage: localStorage,
        paths: ["token"]
      },
      {
        key: "static_base_url",
        storage: sessionStorage,
        paths: ["static_base_url"]
      },
      {
        key: "currency",
        storage: sessionStorage,
        paths: ["currency"]
      },
      {
        key: "lang_support_list",
        storage: sessionStorage,
        paths: ["lang_support_list"]
      },
      {
        key: "active_list",
        storage: sessionStorage,
        paths: ["active_list"]
      },
      {
        key: "loginLayer",
        storage: sessionStorage,
        paths: ["loginLayer"]
      },
      {
        key: "registerActLayer",
        storage: sessionStorage,
        paths: ["registerActLayer"]
      },
      {
        key: "checkActLayer",
        storage: sessionStorage,
        paths: ["checkActLayer"]
      },
      {
        key: "fst_request",
        storage: sessionStorage,
        paths: ["fst_request"]
      },
      {
        key: "recentList",
        storage: localStorage,
        paths: ["recentList"]
      },
      {
        key: "isLoading",
        storage: sessionStorage,
        paths: ["isLoading"]
      },
      {
        key: "logo",
        storage: sessionStorage,
        paths: ["logo"]
      },
      {
        key: "svgData",
        storage: sessionStorage,
        paths: ["svgData"]
      },
      {
        key: "htmlInfo",
        storage: sessionStorage,
        paths: ["htmlInfo"]
      },
      {
        key: "currentGame",
        storage: sessionStorage,
        paths: ["currentGame"]
      },
      {
        key: "isPhoneModel",
        storage: sessionStorage,
        paths: ["isPhoneModel"]
      },
      {
        key: "invit_code",
        storage: localStorage,
        paths: ["invit_code"]
      },
      {
        key: "showService",
        storage: sessionStorage,
        paths: ["showService"]
      },
      {
        key: "customerService",
        storage: sessionStorage,
        paths: ["customerService"]
      },
      {
        key: "timeZone",
        storage: sessionStorage,
        paths: ["timeZone"]
      },
      {
        key: "isApp",
        storage: sessionStorage,
        paths: ["isApp"]
      },
      {
        key: "timeZoneObject",
        storage: sessionStorage,
        paths: ["timeZoneObject"]
      },
      {
        key: "business_list",
        storage: sessionStorage,
        paths: ["business_list"]
      }
    ]
  }
});
